<template>
  <div class="container">
    <el-dialog
      :title="ruleForm.id ? '修改学科' : '新增学科'"
      :visible="centerDialogVisible"
      width="25%"
      @close="closeDialog"
    >
      <span>
        <el-form ref="form" label-width="80px" :model="ruleForm" :rules="rules">
          <el-form-item label="学科名称" prop="subjectName">
            <el-input
              v-model="ruleForm.subjectName"
              placeholder="请输入学科名称"
            ></el-input>
          </el-form-item>
          <el-form-item label="是否显示" prop="isFrontDisplay">
            <el-switch
              v-model="ruleForm.isFrontDisplay"
              active-color="#13ce66"
              inactive-color="#ff4949"
            >
            </el-switch>
          </el-form-item>
        </el-form>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="addNewSubject">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
// 引入接口
import { add, detail, update } from '@/api/hmmm/subjects'
export default {
  props: {
    centerDialogVisible: {
      type: Boolean
    },
    // 修改项的id
    editId: {
      type: Number
    }
  },
  data () {
    return {
      ruleForm: {
        subjectName: '',
        isFrontDisplay: true
      },
      rules: {
        subjectName: [
          { required: true, message: '请输入学科名称', trigger: 'blur' }
        ],
        isFrontDisplay: [{ required: true }]
      }
    }
  },
  methods: {
    // 关闭弹窗
    closeDialog () {
      this.$emit('close-subject-dialog')
      // 清空数据
      this.ruleForm = {
        subjectName: '',
        isFrontDisplay: true
      }
    },
    // 点击确认，发送请求
    addNewSubject () {
      // 先进行兜底校验
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          // 对isFrontDisplay进行判断如果是true的话，返回1，如果是false的话，返回0
          if (this.ruleForm.isFrontDisplay) {
            this.ruleForm.isFrontDisplay = 1
          } else {
            this.ruleForm.isFrontDisplay = 0
          }
          if (this.ruleForm.id) {
            // 如果id存在的话，则是编辑修改
            const data = {
              id: this.ruleForm.id,
              isFrontDisplay: this.ruleForm.isFrontDisplay,
              subjectName: this.ruleForm.subjectName
            }
            await update(data)
            // 提示用户
            this.$message.success('更新成功')
          } else {
            // 如果id不存在的话，则是新增
            const data = {
              subjectName: this.ruleForm.subjectName,
              isFrontDisplay: this.ruleForm.isFrontDisplay
            }
            await add(data)
            // 提示用户
            this.$message.success('新增成功')
          }
          // 关闭弹窗
          this.closeDialog()
          // 更新数据
          this.$emit('update-subject')
        } else {
          return false
        }
      })
    },
    // 点击修改，数据回显
    async editSubject (id) {
      const res = await detail({ id })
      console.log(res.data)
      // 数据回显
      this.ruleForm = res.data
      // 对数据中的isFrontDisplay进行判断，如果是1的话，变成true，反之false
      if (this.ruleForm.isFrontDisplay === 1) {
        this.ruleForm.isFrontDisplay = true
      } else {
        this.ruleForm.isFrontDisplay = false
      }
    }
  }
}
</script>

<style scoped lang='less'></style>
